package com.sgb.kjwl.view.ui.fragment.transport;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.amap.api.maps.model.LatLng;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentCarsInfoBinding;
import com.sgb.kjwl.model.entity.transport.SearchGoodsEntity;
import com.sgb.kjwl.utils.NumberUtil;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.activity.home.SearchActivity4;
import com.sgb.kjwl.view.ui.activity.home.TransportSearchActivity;
import com.sgb.kjwl.view.ui.activity.transport.driver.goods.FreightPrjDetailActivity;
import com.sgb.kjwl.view.ui.activity.transport.driver.goods.GeneralPrjDetailActivity;
import com.sgb.kjwl.view.ui.adapter.home.SearchGoodsInfoAdapter;
import com.sgb.kjwl.viewmodel.transport.SearchGoodsModel;
import com.sgb.link.event.LeaseEvent;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * @author: 王彬臣
 * @date: 2021/2/18
 * @Description: 物流货源搜索
 */
public class SearchTransportGoodsFragment extends AppLazyFragment {
    private FragmentCarsInfoBinding binding;
    SearchGoodsModel viewModel;
    private String searchText;
    private SearchGoodsInfoAdapter goodsInfoAdapter;

    public LatLng currentLatLng = new LatLng(NumberUtil.convertToDouble(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "")),
            NumberUtil.convertToDouble(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "")));

    public static SearchTransportGoodsFragment newInstance() {
        SearchTransportGoodsFragment searchTransportGoodsFragment = new SearchTransportGoodsFragment();
        return searchTransportGoodsFragment;
    }

    public static SearchTransportGoodsFragment newInstance(String keyWord) {
        SearchTransportGoodsFragment fragment = new SearchTransportGoodsFragment();
        Bundle bundle = new Bundle();
        bundle.putString("KEY_WORD", keyWord);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
//        DaggerTransportFragComponent.builder().appComponent(appComponent).transportFragModule(new TransportFragModule(this)).build().inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_cars_info, container, false);
        viewModel = new ViewModelProvider(this).get(SearchGoodsModel.class);
        return binding.getRoot();
    }

    @Override
    protected void onLazyLoad() {

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        if (getArguments() != null) {
            searchText = getArguments().getString("KEY_WORD");
            viewModel.searchCatogories(this, SearchActivity4.SEARCHTYPE_LOGISTICS_GOODS, searchText, false);
        }
        initView();
    }

    public void initView() {
        binding.setLifecycleOwner(this);
        binding.emptyView.setVisibility(View.VISIBLE);
        binding.carsList.setVisibility(View.GONE);
        if (goodsInfoAdapter == null) {
            goodsInfoAdapter = new SearchGoodsInfoAdapter(getActivity(), null);
        }
        binding.carsList.setFootViewText("加载中", "我也是有底线的");
        binding.carsList.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.carsList.setAdapter(goodsInfoAdapter);
        goodsInfoAdapter.setOnItemClick(new AdapterOnItemClick<SearchGoodsEntity>() {
            @Override
            public void onItemClick(SearchGoodsEntity goodsInfoEntity3, int position) {
                if (goodsInfoEntity3.getPattern() == 1) {
                    FreightPrjDetailActivity.start(getActivity(), goodsInfoEntity3.getNo(), false, false);
                } else {
                    GeneralPrjDetailActivity.start(getActivity(), goodsInfoEntity3.getNo(), goodsInfoEntity3.getPattern(), false);
                }

            }
        });
        binding.carsList.setLoadingMoreEnabled(true);
        binding.carsList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                viewModel.searchCatogories(SearchTransportGoodsFragment.this, SearchActivity4.SEARCHTYPE_LOGISTICS_GOODS, searchText, false);
            }

            @Override
            public void onLoadMore() {

                viewModel.searchCatogories(SearchTransportGoodsFragment.this, SearchActivity4.SEARCHTYPE_LOGISTICS_GOODS, searchText, true);
            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LeaseEvent event) {
        if (event.getMessageTag() == EventConstant.SEARCH_GOODS) {
            searchText = (String) event.getMessageData();
            if (this.getActivity() instanceof SearchActivity4 || this.getActivity() instanceof TransportSearchActivity) {
                if (goodsInfoAdapter != null) {
                    goodsInfoAdapter.setDatas(null);
                }
                viewModel.searchCatogories(this, SearchActivity4.SEARCHTYPE_LOGISTICS_GOODS, searchText, false);
            }
            EventBus.getDefault().removeStickyEvent(event);
        }
    }

    //加载货物列表
    public void refreshGoodsInfoList(List<SearchGoodsEntity> list, int totalRow, boolean isLoadMore) {
        binding.carsList.refreshComplete();
        if (isLoadMore) {
            binding.carsList.loadMoreComplete();
            if (list != null && list.size() > 0) {
                goodsInfoAdapter.addDatas(list);
                binding.carsList.setNoMore(false);
            } else {
                binding.carsList.setNoMore(true);
            }
            binding.carsList.setVisibility(View.VISIBLE);
        } else {
            binding.carsList.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.carsList.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                goodsInfoAdapter.setDatas(list);
                binding.carsList.setLoadingMoreEnabled(true);
            } else {
                goodsInfoAdapter.setDatas(null);
                binding.carsList.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }

        EventBus.getDefault().postSticky(LeaseEvent.make().setMessageTag(EventConstant.EVENT_GOODS_COUNT).setCount(totalRow));

    }

    public void refreshChildView(String mKeyWord) {
        viewModel.searchCatogories(SearchTransportGoodsFragment.this, SearchActivity4.SEARCHTYPE_LOGISTICS_GOODS, mKeyWord, false);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
